
const express = require("express");

const { db } = require("../database");

const { validate, validators } = require("../validate");
// const { route } = require("express/lib/router");


const router = express.Router();

const { useAuthorization } = require("./base");
// router.use(useAuthorization);

router

    // 增加文档
    .post(
        "/add",
        async (req, res) => {
            const { title, description, content, createtime, creator, workflow, processed, status } = req.body;
            const id = await db.insert(
                "INSERT INTO document (title, description, content, creator, createtime, workflow, processed, status) VALUES (?, ?, ?, ?, ?, ?, ?,?)",
                [title, description, content, creator, createtime, workflow, processed, status]
            );

            const document = await db.findOne`SELECT * FROM document `;
            res.send({
                code: 200,
                message: "文档创建成功",
                result: document,
            });
        }
    )
    //修改文档
    .put(
        "/modify",
        async (req, res) => {
            // console.log(1);
            let document =
                await db.findOne`select * from document `;
            if (!document) {
                return res.send({ code: -1, message: "该文档信息不存在！" });
            }
            const { id, title, description, content, creator, processed, status } = req.body;
            // if(title==){

            // }
            await db.run`update document set title = ${title}, description = ${description}

          ,content= ${content}
          ,creator = ${creator}
          ,processed = ${processed}
          ,status=${status} WHERE id=${id}`;
            document = await db.findOne`select * from document WHERE creator=${creator}`;
            res.send({
                code: 200,
                message: "修改成功",
                result: document
            });
        })

    //查询文档
    .put("/query", async (req, res) => {
        const { creator } = req.body;
        const document =
            await db.findOne`select * from document  WHERE creator=${creator} `;
        if (!document) {
            return res.send({ code: -1, message: "该文档信息不存在！" });
        }
        res.send({
            code: 200,
            message: "文档查询成功",
            result: document,
        });
    })

    //删除文档
    .delete("/delete",
        async (req, res) => {
            const { creator } = req.body;
            const document =
                await db.findOne`select * from document WHERE creator=${creator}`;
            if (!document) {
                return res.send({ code: -1, message: "该文档信息不存在！" });
            }
            // 删除 document 记录
            await db.run` DELETE FROM document WHERE creator=${creator}`;
            res.send({
                code: 200,
                message: "删除成功",
                result: document
            });
        })
router.get('/',
    async (req, res) => {
        const document = await db.select(`select * from document`);
        if (document.length < 1) {
            return res.send({
                code: 201,
                message: "暂时没有文件",

            })
        }
        else {
            return res.send({
                code: 200,
                message: "获取文件成功",
                result: document
            })
        }
    }
)
router.get('/status',
    async (req, res) => {
        const { status } = req.body
        const document = await db.select(`select * from document where status=${status}`);
        if (document.length < 1) {
            return res.send({
                code: 201,
                message: "暂时没有文件",

            })
        }
        else {
            return res.send({
                code: 200,
                message: "获取文件成功",
                result: document
            })
        }
    }
)

    //按条件查询员工
    .get("/postperson",
        async (req, res) => {
            const { query } = req.query;
            const person = await db.select(`select*from person where name like "${"%" + query + "%"}" or org like "${"%" + query + "%"}" or phone like "${"%" + query + "%"}";`)
            res.send({ code: 200, message: "查询成功", result: person })
        })


const registerRouter = (app) => app.use("/document", router);

module.exports = { registerRouter };
